#region File Description
//-----------------------------------------------------------------------------
// MessageBoxScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using EventFlowLibrary.Player;
using System.Collections.Generic;
#endregion

namespace ScreenManagement
{
    /// <summary>
    /// A popup message box screen, used to display "are you sure?"
    /// confirmation messages.
    /// </summary>
    class MessageBoxScreen : GameScreen
    {
        #region Fields

        List<string> messages;

        float widestLine = 0;

        bool includeAButton, includeBButton, exitOnAccept, exitOnCancel;
        Texture2D gradientTexture, aButton, bButton;

        #endregion

        #region Events

        public event EventHandler<PlayerIndexEventArgs> Accepted;
        public event EventHandler<PlayerIndexEventArgs> Cancelled;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor lets the caller specify whether to include the standard
        /// "A=ok, B=cancel" usage text prompt.
        /// </summary>
        /// <param name="_exitOnAccept">Whether to exit when accept is pressed, along with other functions. Default should be true.</param>
        /// <param name="_exitOnCancel">Whether to exit when cancel is pressed, along with other functions. Default should be true.</param>
        public MessageBoxScreen(string[] _message, bool _includeAButton, bool _includeBButton, bool _exitOnAccept, bool _exitOnCancel)
        {
            exitOnAccept = _exitOnAccept;
            exitOnCancel = _exitOnCancel;
            includeAButton = _includeAButton;
            includeBButton = _includeBButton;

            // Find the width of the longest string
            messages = new List<string>();
            if (_message.Length > 0)
            {
                for (int i = 0; i < _message.Length; i++)
                {
                    messages.Add(_message[i]);
                }
            }

            // show button text
            if (includeAButton)
            {
                messages.Add("    = OK");
            }
            if (includeBButton)
            {
                messages.Add("    = Cancel");
            }

            IsPopup = true;

            TransitionOnTime = TimeSpan.FromSeconds(0.2);
            TransitionOffTime = TimeSpan.FromSeconds(0.2);
        }


        /// <summary>
        /// Loads graphics content for this screen. This uses the shared ContentManager
        /// provided by the Game class, so the content will remain loaded forever.
        /// Whenever a subsequent MessageBoxScreen tries to load this same content,
        /// it will just get back another reference to the already loaded data.
        /// </summary>
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            gradientTexture = content.Load<Texture2D>("Textures/Screens/gradient");
            aButton = content.Load<Texture2D>("Textures/Buttons/xboxControllerButtonA");
            bButton = content.Load<Texture2D>("Textures/Buttons/xboxControllerButtonB");

            base.LoadContent();

            // Find the width of the longest string
            if (messages.Count > 0)
            {
                widestLine = ScreenManager.FontLarge.MeasureString(messages[0]).X;
                for (int i = 0; i < messages.Count; i++)
                {
                    if (ScreenManager.FontLarge.MeasureString(messages[i]).X > widestLine)
                        widestLine = ScreenManager.FontLarge.MeasureString(messages[i]).X;
                }
            }
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Responds to user input, accepting or cancelling the message box.
        /// </summary>
        public override void HandleInput(GameTime _gameTime, InputState input)
        {
            PlayerIndex playerIndex;

            // We pass in our ControllingPlayer, which may either be null (to
            // accept input from any player) or a specific index. If we pass a null
            // controlling player, the InputState helper returns to us which player
            // actually provided the input. We pass that through to our Accepted and
            // Cancelled events, so they can tell which player triggered them.
            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                // Raise the accepted event, then exit the message box.
                if (Accepted != null)
                    Accepted(this, new PlayerIndexEventArgs(playerIndex));

                if (exitOnAccept)
                    ExitScreen();
            }
            else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                // Raise the cancelled event, then exit the message box.
                if (Cancelled != null)
                    Cancelled(this, new PlayerIndexEventArgs(playerIndex));

                if (exitOnCancel)
                    ExitScreen();
            }
        }


        #endregion

        #region Draw


        /// <summary>
        /// Draws the message box.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            // Darken down any other screens that were drawn beneath the popup.
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);

            // Center the message text in the viewport.
            Vector2 viewportSize = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height);

            Vector2 textSize = new Vector2(widestLine, (messages.Count) * ScreenManager.FontLarge.LineSpacing);
            //Vector2 textSize = ScreenManager.FontLarge.MeasureString(message);
            textSize.X += 32;
            Vector2 textPosition = (viewportSize - textSize) / 2;

            // The background includes a border somewhat larger than the text itself.
            const int hPad = 32;
            const int vPad = 16;

            Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
                                                          (int)textPosition.Y - vPad,
                                                          (int)textSize.X + hPad * 2,
                                                          (int)textSize.Y + vPad * 2);

            // Fade the popup alpha during transitions.
            Color color = Color.White * TransitionAlpha;

            spriteBatch.Begin();

            // Draw the background rectangle.
            spriteBatch.Draw(gradientTexture, backgroundRectangle, color);

            // Draw the buttons
            if (includeAButton && includeBButton)
            {
                spriteBatch.Draw(aButton, new Rectangle((int)textPosition.X, (int)textPosition.Y + (ScreenManager.FontLarge.LineSpacing * (messages.Count - 2)) + 7, 32, 32), Color.White);
                spriteBatch.Draw(bButton, new Rectangle((int)textPosition.X, (int)textPosition.Y + (ScreenManager.FontLarge.LineSpacing * (messages.Count - 1)) + 7, 32, 32), Color.White);
            }
            else
            {
                if (includeAButton)
                    spriteBatch.Draw(aButton, new Rectangle((int)textPosition.X, (int)textPosition.Y + (ScreenManager.FontLarge.LineSpacing * (messages.Count - 1)) + 7, 32, 32), Color.White);
                if (includeBButton)
                    spriteBatch.Draw(bButton, new Rectangle((int)textPosition.X, (int)textPosition.Y + (ScreenManager.FontLarge.LineSpacing * (messages.Count - 1)) + 7, 32, 32), Color.White);
            }

            // Draw the message box text.
            for (int i = 0; i < messages.Count; i++)
            {
                spriteBatch.DrawString(ScreenManager.FontLarge, messages[i], new Vector2(textPosition.X, textPosition.Y + (i * ScreenManager.FontLarge.LineSpacing)), color);
            }

            spriteBatch.End();
        }


        #endregion
    }
}
